/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_client_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleSsapClientProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    if(code != SLE_SSAP_CLIENT_WRITE_REQUEST && code != SLE_SSAP_CLIENT_WRITE_COMMAND) {
        SLE_LOGD("InnerTransact In");
    }
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

SleErrorCode SleSsapClientProxy::RegisterSsapClient(const Uuid &uuid, uint8_t &clientId, const sptr<ISleSsapClientCallback> &callback)
{
    SLE_LOGD("RegisterSsapClient In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&uuid), SLE_ERR_IPC_TRANS_FAILED, "write client uuid failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), SLE_ERR_IPC_TRANS_FAILED, "write client callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_REGISTER_CLIENT, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "register ssap client failed!(%d)", exception);
    clientId = reply.ReadUint8();
    return (SleErrorCode)exception;
}

SleErrorCode SleSsapClientProxy::UnregisterSsapClient(uint8_t clientId, const sptr<ISleSsapClientCallback> &callback)
{
    SLE_LOGD("UnregisterSsapClient In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), SLE_ERR_IPC_TRANS_FAILED, "write client callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_UNREGISTER_CLIENT, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapClientProxy::FindSsapStructure(uint8_t clientId, uint16_t connectId, const SsapClientFindStructure &param)
{
    SLE_LOGD("FindSsapStructure In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write find structure param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_FIND_STRUCTURE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapClientProxy::ReadRequestByUuid(uint8_t clientId, uint16_t connectId, const SsapClientReadReqByUUID &param)
{
    SLE_LOGD("ReadRequestByUuid In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write read param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_READ_REQUEST_BY_UUID, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapClientProxy::ReadRequest(uint8_t clientId, uint16_t connectId, uint16_t handle, uint8_t type)
{
    SLE_LOGD("ReadRequest In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(handle), SLE_ERR_IPC_TRANS_FAILED, "write read handle failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(type), SLE_ERR_IPC_TRANS_FAILED, "write read type failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_READ_REQUEST, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapClientProxy::WriteRequest(uint8_t clientId, uint16_t connectId, const SsapClientHandleValue &param)
{
#if 0
    SLE_LOGD("WriteRequest In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write write param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_WRITE_REQUEST, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapClientProxy::WriteCommand(uint8_t clientId, uint16_t connectId, const SsapClientHandleValue &param)
{
#if 0
    SLE_LOGD("WriteCommand In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write write param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_WRITE_COMMAND, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapClientProxy::ExchangeInfo(uint8_t clientId, uint16_t connectId, uint32_t mtu, uint16_t version)
{
    SLE_LOGD("ExchangeInfo In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapClientProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(clientId), SLE_ERR_IPC_TRANS_FAILED, "write client id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint32(mtu), SLE_ERR_IPC_TRANS_FAILED, "write mtu failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(version), SLE_ERR_IPC_TRANS_FAILED, "write version failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapClientInterfaceCode::SLE_SSAP_CLIENT_EXCHANGE_REQUEST, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}
} // SleStandard
} // NearLink
} // OHOS